package com.cqeec.ui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.cqeec.action.UserAction;
import com.cqeec.bean.ResultMessage;
import com.cqeec.bean.UserInfo;

/**
 * 
 * @author HuaL
 * @description 登录窗体
 */
public class LoginFrame extends JFrame implements ActionListener{
	JLabel login_img;
	JTextField txt_user;
	JPasswordField txt_pwd;
	JButton btn_login;
	ImageIcon image;
	public LoginFrame(){
		Init();
	}
	
	public void Init(){
		setLayout(null);
		setSize(770, 314);
		setTitle("用户登录");
		setResizable(false);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		txt_user = new JTextField(20);
		txt_user.setBounds(460, 145, 180, 28);
		add(txt_user);
		
		txt_pwd = new JPasswordField(20);
		txt_pwd.setBounds(460, 190, 180, 28);
		txt_pwd.setEchoChar('#');
		add(txt_pwd);
		
		btn_login = new JButton();
		image = new ImageIcon("Image\\btn_login.jpg");
		btn_login.setIcon(image);
		btn_login.setBounds(660, 134, 88, 88);
		btn_login.addActionListener(this);
		add(btn_login);
		
		image = new ImageIcon("Image\\login.jpg");
		login_img = new JLabel();
		login_img.setOpaque(true);
		login_img.setIcon(image);
		login_img.setBounds(0, 0, 770, 314);
		add(login_img);
		
		setVisible(true);
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new LoginFrame();
	}

	public void actionPerformed(ActionEvent e) {
		if (txt_user.getText().trim().equals("")){
			JOptionPane.showMessageDialog(null, "请输入登录用户名", "登录信息",JOptionPane.WARNING_MESSAGE);
			txt_user.requestFocusInWindow();
			return;
		}
		if (txt_pwd.getText().trim().equals("")){
			JOptionPane.showMessageDialog(null, "请输入登录密码", "登录信息",JOptionPane.WARNING_MESSAGE);
			txt_pwd.requestFocusInWindow();
			return;
		}
		UserInfo userInfo = new UserInfo();
		userInfo.setUsername(txt_user.getText().trim());
		userInfo.setUserpwd(txt_pwd.getText().trim());
		UserAction action = new UserAction();
		ResultMessage resultMessage = action.Login(userInfo);
		if (resultMessage.getResultCode() == 0){
			//将登录用户的信息保存到userInfo对象中
			setVisible(false);
			new MainFrame(resultMessage.getUserInfo());
		}else if (resultMessage.getResultCode() == -1){
			JOptionPane.showMessageDialog(null, "用户名错误", "登录信息",JOptionPane.ERROR_MESSAGE);
			txt_user.requestFocusInWindow();
			return;
		}else if (resultMessage.getResultCode() == -2){
			JOptionPane.showMessageDialog(null, "密码错误", "登录信息",JOptionPane.ERROR_MESSAGE);
			txt_pwd.requestFocusInWindow();
			return;
		}else if (resultMessage.getResultCode() == -3){
			JOptionPane.showMessageDialog(null, "系统错误，请联系系统管理员", "登录信息",JOptionPane.ERROR_MESSAGE);
			return;
		}
	}

}
